package org.example.kafka;


import com.alibaba.fastjson2.JSONObject;
import org.example.util.HutoolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class MessageProducer {
    @Autowired
    private HutoolUtil hutoolUtil;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    private static final Logger logger = LoggerFactory.getLogger(MessageProducer.class);
    public void send(Event event){
        if (event == null){
            logger.info("kafka生产者发送消息为空!!");
            return;
        }
        if (hutoolUtil.stringIsEmpty(event.getTopic())){
            logger.info("kafka生产者不知道发送那个topic !!");
            return;
        }

        String s = JSONObject.toJSONString(event);
        kafkaTemplate.send(event.getTopic(), s);
    }
    //每次写代码都有可能不一样  想法可能完善也可能缺漏
    public int sendToEs(DiscussPostEvent event){
        if (event==null) {
            logger.info("帖子事件对象为null");
            return 0;
        };
        if (hutoolUtil.stringIsEmpty(event.getTopic()) || event.getDiscussPost() == null){
            logger.info("事件对象中主题 或者 帖子为空");
            return 0;
        }
        String s = JSONObject.toJSONString(event.getDiscussPost());
        kafkaTemplate.send(event.getTopic(),s);
        return 1;
    }
    public int deleteDiscuss(String topic,String id){
        if (hutoolUtil.stringIsEmpty(topic) || hutoolUtil.stringIsEmpty(id)){
            logger.info("发布帖子异常，主题或者帖子id有问题");
            return 0;
        }
        kafkaTemplate.send(topic,id);
        return 1;
    }
    public int wkGenerateWebImage(WKEvent wkEvent){
        if (wkEvent == null || hutoolUtil.stringIsEmpty(wkEvent.getWebLocation()) || wkEvent.getUserId()==null){
            logger.info("处理wkGenerateWebImage 时间对象异常");
            return 0;
        }
        String s = JSONObject.toJSONString(wkEvent);
        kafkaTemplate.send(wkEvent.getTopic(),s);
        return 2;
    }
}
